#!/bin/bash

LINGLONG_ROOT="/var/lib/linglong"
VERSION=0

if [ -f /etc/os-release ]; then
        VERSION=$(sed -ne '/^VERSION_ID=.*$/P' /etc/os-release | awk -F = '{print $2}' | sed -e 's/"//g' | awk -F . '{print $1}')

        if [ -z "${VERSION}" ]; then
                VERSION=0
        fi

        if [ ${VERSION} -eq 20 ]; then
                LINGLONG_ROOT="/data/linglong"
        elif [ ${VERSION} -eq 23 ]; then
                LINGLONG_ROOT="/persistent/linglong"
        fi
fi

new_dirs=
while read -r install_path; do
        share_path=$install_path/entries/share
        case ":$XDG_DATA_DIRS:" in
        *":$share_path:"*) : ;;
        *":$share_path/:"*) : ;;
        *) new_dirs=${new_dirs:+${new_dirs}:}$share_path ;;
        esac
done < <(
        echo "${XDG_DATA_HOME:-"$HOME/.local/share"}/linglong"
        echo "${LINGLONG_ROOT}"
)

XDG_DATA_DIRS="${new_dirs:+${new_dirs}:}${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
echo "XDG_DATA_DIRS=$XDG_DATA_DIRS"
echo LINGLONG_ROOT=${LINGLONG_ROOT}
